//
//  AppDelegate.m
//  BCManagerVersion
//
//  Created by 吴 崧 on 14-2-22.
//  Copyright (c) 2014年 Jungle. All rights reserved.
//

#import "AppDelegate.h"


#import "BCUserConfigViewController.h"
#import "BCHomePageViewController.h"
//chat Controller
#import "BCBankViewController.h"
#import "BCNewsViewController.h"
#import "BCLoginViewController.h"
#import "BCFriendsViewController.h"

NSString * const  BCDeviceTokenKey = @"BCDeviceTokenKey";
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    self.userManager = [UserManager sharedManager];
   
    self.window.rootViewController = [self createContainer];



    [self.window makeKeyAndVisible];
    
    [application registerForRemoteNotificationTypes:
     UIRemoteNotificationTypeBadge |
     UIRemoteNotificationTypeAlert |
     UIRemoteNotificationTypeSound];
    
    return YES;


}


- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSString *devStr = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
	NSLog(@"动态令牌 %@",devStr);

    [[NSUserDefaults standardUserDefaults] setObject:devStr forKey:BCDeviceTokenKey];
    
    if (USER_MANAGER.auth.length > 0) {
        [BCNetworkClient bindPhoneId:devStr auth:USER_MANAGER.auth success:^{
            
        } failure:^(NSError *error) {
            
        }];
    }

	return;
} 



-(UITabBarController *)createContainer
{
    // Custom initialization

    
    NSMutableArray *vcs = [[NSMutableArray alloc]initWithCapacity:5];
    BCHomePageViewController *homPage = [[BCHomePageViewController alloc]init];
    homPage.title = @"主页";
    

    homPage.tabBarItem.image = [UIImage imageNamed:@"home"];
    homPage.tabBarItem.selectedImage = [UIImage imageNamed:@"home_sel"];
    UINavigationController *homeNav = [[UINavigationController alloc]initWithRootViewController:homPage];
    [vcs addObject:homeNav];
    
    
    BCNewsViewController* news = [[BCNewsViewController alloc] init];
    news.title = @"快讯";
    news.tabBarItem.image = [UIImage imageNamed:@"news"];
    news.tabBarItem.selectedImage = [UIImage imageNamed:@"news_sel"];
    [vcs addObject:news];
    
    
    BCFriendsViewController *chat = [[BCFriendsViewController alloc] init];
    chat.title =@"米圈";
    chat.tabBarItem.image = [UIImage imageNamed:@"LM"];
    chat.tabBarItem.selectedImage = [UIImage imageNamed:@"LM_sel"];

    UINavigationController *chatNav = [[UINavigationController alloc]initWithRootViewController:chat];
    [vcs addObject:chatNav];
    
    
    BCBankViewController *bank = [[BCBankViewController alloc]init];
    bank.title = @"金融中心";
    bank.tabBarItem.image = [UIImage imageNamed:@"bank"];
    bank.tabBarItem.selectedImage = [UIImage imageNamed:@"bank_sel"];
   
    UINavigationController *banNav = [[UINavigationController alloc]initWithRootViewController:bank];
    [vcs addObject:banNav];
    
    BCUserConfigViewController *user = [[BCUserConfigViewController alloc]init];
    user.title = @"我的";
    user.tabBarItem.image = [UIImage imageNamed:@"user"];
    user.tabBarItem.selectedImage = [UIImage imageNamed:@"user_sel"];
    UINavigationController *userNav = [[UINavigationController alloc]initWithRootViewController:user];
    [vcs addObject:userNav];
    
    
    //tab bar Title Attribute
     [[UITabBarItem appearance]setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:10],NSFontAttributeName,[UIColor colorWithRed:241.0/255 green:241.0/255 blue:241.0/255 alpha:1],NSForegroundColorAttributeName,nil] forState:UIControlStateNormal];
    
    
    
    //custom init&&Config TabBarController
    UITabBarController *tabVC = [[UITabBarController alloc]init];
    tabVC.view.frame = CGRectSetY(tabVC.view.frame, iOS7_HEIGHT);
    tabVC.viewControllers = vcs;
    [tabVC.tabBar setBackgroundImage:[UIImage imageNamed:@"containner_tab"]];
    tabVC.delegate = self;
      tabVC.tabBar.tintColor = [UIColor colorWithRed:208.0/255 green:71.0/255 blue:63.0/255 alpha:1];
    
    return tabVC;

}


-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
   
    if (!USER_MANAGER.didLogin &&
        ([viewController isEqual:[tabBarController.viewControllers lastObject]]
         || [viewController isEqual:tabBarController.viewControllers[2]]||[viewController isEqual:tabBarController.viewControllers[1]])
        ) {
        [BCLoginViewController checkLoggedInWithBlock:^(BOOL isLoggedIn) {
            if (isLoggedIn) {
                [tabBarController setSelectedViewController:viewController];
            }else
            {
                [tabBarController setSelectedViewController:[tabBarController.viewControllers firstObject]];
                
            }
        }];
        return NO;
    }
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}



@end
